<?php

class WSB_Controller_Action2 extends Zend_Controller_Action
{

    /**
     * @var WSB_DbMapper
     */
    protected $_mapper;

    /**
     * @var array
     */
    protected $_columns;

    /**
     * @var string
     */
    protected $_defSortField;

    /**
     * @var string
     */
    protected $_defSortDirection;

    
    public function init()
    {

        // Konfigurationseinstellungen aus $GLOBALS['WSB']['controllers']
        $config = $GLOBALS['WSB']['controllers'][get_class($this)];
        $this->_mapper = new $config['mapperClass']();
        $this->_columns = $config['columns'];
        $this->_defSortField = $config['defSortField'];
        $this->_defSortDirection = $config['defSortDirection'];

        $translate = Zend_Registry::get('Zend_Translate');
        $locale = Zend_Registry::get('Zend_Locale');
        foreach($config['translations'] AS $translation) {
            $translationFile = sprintf($translation, $locale);
            $translate->addTranslation($translationFile, $locale);
        }

        $ajaxContext = $this->getHelper('AjaxContext');
        $ajaxContext->addActionContext('enable', 'json')
                    ->addActionContext('disable', 'json')
                    ->initContext();
        $this->_helper->layout->setLayout('test');

        $session = $this->getHelper('Session');
        if(empty($session->sortField)) {
            $session->sortField = $this->_defSortField;
        }
        if(empty($session->sortDirection)) {
            $session->sortDirection = $this->_defSortDirection;
        }
        if(empty($session->filter)) {
            $session->filter = '';
        }
    }

    public function indexAction()
    {
        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $session = $this->getHelper('Session');
        
        // UI
        $page = $request->getParam('page', 1);
        $sortField = $request->getParam('sort', $session->sortField);
        $sortDirection = $request->getParam('dir', $session->sortDirection);
        
        $filter = $request->getPost('filter', $session->filter);
        $reset = $request->getPost('reset');
        if(!empty($reset)) {
            $filter = '';
        }

        $paginator = $mapper->getPaginator($page, $sortField . ' ' . $sortDirection, $filter);
        $items = $paginator->getCurrentItems();

        $collectionClass = $mapper->getCollectionClass();

        $this->view->paginator = $paginator;
        $this->view->columns = $this->_columns;
        $this->view->collection = new $collectionClass($items);
        $this->view->sortField = $sortField;
        $this->view->sortDirection = $sortDirection;
        $this->view->filter = $filter;

        $session->sortField = $sortField;
        $session->sortDirection = $sortDirection;
        $session->filter = $filter;
    }

    public function addAction()
    {
        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $redirector = $this->getHelper('redirector');

        // Cancel
        if($request->getPost('_cancel') != '') {
            $redirector->gotoRoute(
                array(
                    'action' => 'index'
                )
            );
        }

        $modelClass = $mapper->getModelClass();
        $model = new $modelClass();
        $form = $model->getForm();

        if ($request->isPost()) {

            if ($form->isValid($request->getPost())) {

                $values = $form->getValues();
                unset($values['_save'], $values['_saveAndClose'], $values['_cancel']);
                $model->populate($values);
                $mapper->save($model);

                $this->_helper->FlashMessenger->addMessage($this->_helper->Translate('entry was saved'));

                // Save and Close
                if($request->getPost('_saveAndClose') != '') {
                    $redirector->gotoRoute(
                        array(
                            'action' => 'index',
                            'id' => null
                        )
                    );
                }

                $redirector->gotoRoute(
                    array(
                        'action' => 'edit',
                        'id' => $model->id
                    )
                );

            }

        }

        $this->view->form = $form;

    }

    public function editAction()
    {
        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $redirector = $this->getHelper('redirector');

        // Cancel
        if($request->getPost('_cancel') != '') {
            $redirector->gotoRoute(
                array(
                    'action' => 'index',
                    'id' => null
                )
            );
        }

        $id = intval($request->getParam('id'));
        if($id > 0) {
            $model = $mapper->find($id);

            $form = $model->getForm();

            if ($request->isPost()) {

                if ($form->isValid($request->getPost())) {
                    $values = $form->getValues();
                    unset($values['_save'], $values['_saveAndClose'], $values['_cancel']);
                    $model->populate($values);
                    $mapper->save($model);

                    $this->_helper->FlashMessenger->addMessage($this->_helper->Translate('entry was saved'));

                    // Save and Close
                    if($request->getPost('_saveAndClose') != '') {
                        $redirector->gotoRoute(
                            array(
                                'action' => 'index',
                                'id' => null
                            )
                        );
                    }

                }

            } else {
                $form->setDefaults($model->toArray(true));
            }

            $this->view->form = $form;

        }
    }

    public function saveAction()
    {
        
    }

    public function deleteAction()
    {
        $this->getHelper('viewRenderer')->setNoRender(true);

        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $redirector = $this->getHelper('redirector');

        $id = $request->getParam('id');
        if(ctype_digit($id)) {
            $model = $mapper->find($id);
            if(isset($model->deleted)) {
                $model->deleted = 1;
                $mapper->save($model);
            }
        }

        $this->_forward('index');
    }

    public function enableAction()
    {
        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $redirector = $this->getHelper('redirector');

        $id = $request->getParam('id');
        if(ctype_digit($id)) {
            $model = $mapper->find($id);
            if(isset($model->hidden)) {
                $model->hidden = 0;
                $mapper->save($model);
            }
        }

        if($request->isXmlHttpRequest()) {
            exit;
        } else {
            $redirector->gotoRoute(
                array(
                    'action' => 'index',
                    'disable' => null,
                    'id' => null
                )
            );
        }
    }

    public function disableAction()
    {
        $request = $this->getRequest();
        $mapper = $this->getMapper();
        $redirector = $this->getHelper('redirector');

        $id = $request->getParam('id');
        if(ctype_digit($id)) {
            $model = $mapper->find($id);
            if(isset($model->hidden)) {
                $model->hidden = 1;
                $mapper->save($model);
            }
        }

        if($request->isXmlHttpRequest()) {
            exit;
        } else {
            $this->getHelper('redirector')->gotoRoute(
                array(
                    'action' => 'index',
                    'disable' => null,
                    'id' => null
                )
            );
        }
    }

    public function getMapper()
    {
        return $this->_mapper;
    }

}
